package project.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import project.entity.TGl;
import project.service.TGlService;
import project.util.ResponseUtil;
import project.util.UUIDUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 攻略表 前端控制器
 * </p>
 *
 * @author YJR
 * @since 2023-07-26
 */
@RestController
@RequestMapping("/api/gl/v1")
public class TGlController {
    @Resource
    private TGlService glService;

    /**
     * 获取轮播图
     *
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    public JSONObject getAll() {
        QueryWrapper<TGl> wrapper = new QueryWrapper<>();
        List<TGl> list = glService.list(wrapper);
        return ResponseUtil.getSuccessResponseBody(list);
    }

    /**
     * 添加或者更轮播图
     *
     * @param gl
     * @return
     */
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    public JSONObject addOrUpdate(@RequestBody TGl gl) {
        if (StringUtils.isBlank(gl.getcId())) {
            gl.setcId(UUIDUtil.getUUID());
        }
        return ResponseUtil.getSuccessResponseBody(glService.saveOrUpdate(gl));

    }


    /**
     * 分页查询数据
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public JSONObject selectSpfl(@RequestBody JSONObject param) {
        // 获取页数
        Integer current = param.getInteger("current");
        // 获取轮播图名
        String glmc = param.getString("glmc");
        Page<TGl> page = new Page<TGl>();
        page.setSize(10);
        if (current != null) {
            page.setCurrent(current);
        } else {
            page.setCurrent(1);
        }

        QueryWrapper<TGl> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(glmc)) {
            wrapper.lambda().like(TGl::getcName, glmc);
        }
        // 获取查询返回结果
        Page<TGl> pageSelect = glService.page(page, wrapper);
        JSONObject object = new JSONObject();
        object.put("list", pageSelect.getRecords());
        object.put("total", pageSelect.getTotal());
        return ResponseUtil.getSuccessResponseBody(object);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public boolean del(@RequestBody TGl gl) {
        return glService.removeById(gl);
    }

    @RequestMapping(value = "/wx/getByid", method = RequestMethod.POST)
    public JSONObject getById(@RequestBody TGl gl) {
        return ResponseUtil.getSuccessResponseBody(glService.getById(gl.getcId()));
    }

    @RequestMapping(value = "/wx/select", method = RequestMethod.POST)
    public List<TGl> wxSelect(@RequestBody JSONObject param) {
        QueryWrapper<TGl> wrapper = new QueryWrapper<>();
        return glService.list(wrapper);
    }
}
